package com.kwamecorp.brunoperry.kwamelogo;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;

import com.kwamecorp.brunoperry.kwamelogo.engine.KCCamera3D;
import com.kwamecorp.brunoperry.kwamelogo.engine.KCGLSurfaceView;
import com.kwamecorp.brunoperry.kwamelogo.engine.KCListener;
import com.kwamecorp.brunoperry.kwamelogo.engine.KCModel3D;
import com.kwamecorp.brunoperry.kwamelogo.engine.io.KCIO;
import com.kwamecorp.brunoperry.kwamelogo.engine.math.Vector3D;

public class MainActivity extends Activity
{
	private KCGLSurfaceView view;
	private KCModel3D logoMesh;
	private KCCamera3D camera;
	private KCIO io;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		init();
	}

	private void init()
	{
		view = new KCGLSurfaceView(this);
		setContentView(view);

		io = new KCIO();

		camera = new KCCamera3D();

		logoMesh = new KCModel3D(this);
		logoMesh.setOnCompleteListener(new KCListener()
		{

			@Override
			public void onComplete()
			{
				view.addModel(logoMesh);
				view.addCamera(camera);
			}

			@Override
			public void onSurfaceCreated()
			{
			}
		});
		logoMesh.loadOBJ(R.raw.logo_mesh);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		int action = event.getAction();

		switch (action)
		{
		case MotionEvent.ACTION_DOWN:

			io.setPress(event.getX(), event.getY());
			camera.translate(new Vector3D(0, 0, 15));
			break;
		case MotionEvent.ACTION_UP:
			camera.translate(new Vector3D(0, 0, 20));
			break;
		case MotionEvent.ACTION_MOVE:

			Vector3D movePos = io.getMove(event.getX(), event.getY());

			logoMesh.setRotation(movePos.x, 1, 0, 0);
			logoMesh.setRotation(movePos.y, 0, 1, 0);
			break;

		default:
			break;
		}

		return true;
	}

	@Override
	protected void onResume()
	{
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		view.onPause();
	}
}
